package com.cms.manage.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author sgy
 */
public class DateUtil {

    public static void main(String[] args) {

        System.out.println(nowAfter("2020-02-05 14:00:00"));
        System.out.println(nowAfter("2020-02-05 15:00:00"));
        System.out.println(nowAfter("2020-02-05 16:00:00"));
        System.out.println(nowAfter("2020-02-05 17:00:00"));

    }

    //获取当天时间
    public static String getToday() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dt = sdf.format(date);
        return dt;
    }

    public static String getYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    //根据时间获取年月日 时分秒
    public static String getDateYMDHMS(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmss");
        return sdf.format(date);
    }

    public static String getNowYMDHMS() {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmss");
        return sdf.format(new Date());
    }

    public static String getNowSpiltYMDHMS() {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    //根据时间获取年月日
    public static String getDateYMD(Date date) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) + 1; // 0-based!
        int day = now.get(Calendar.DAY_OF_MONTH);
        return year + "-" + month + "-" + day;
    }

    //根据时间获取年月
    public static String getDateYM(Date date) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) + 1; // 0-based!
        int day = now.get(Calendar.DAY_OF_MONTH);
        return year + "-" + month;
    }

    //获取当天时间
    public static String getStrByDateType(Date date, String type) {
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        String dt = sdf.format(date);
        return dt;
    }

    //根据字符串时间获取日期
    public static Date getDateByStrType(String date, String type) {
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        Date dateParse = new Date();
        try {
            dateParse = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        return dateParse;
    }

    //根据字符串时间获取上count个月
    public static String getLastDateByDate(String currentDate, int count, String currentType, String lastType) {
        Date date = DateUtil.getDateByStrType(currentDate, currentType);
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(now.MONTH, -count);
        SimpleDateFormat dft = new SimpleDateFormat(lastType);
        String lastMonth = dft.format(now.getTime());
        return lastMonth;
    }

    /**
     * @return 今天 00:00:00
     */
    public static Date getDateTodayFirstHMS() {
        return getDateFirstHMS(new Date());
    }

    /**
     * @return 今天 23:59:59
     */
    public static Date getDateTodayLastHMS() {
        return getDateLastHMS(new Date());
    }

    public static Date getDateFirstHMS(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = sdf.format(date);
        try {
            return sdf2.parse(s + " 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date getDateLastHMS(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = sdf.format(date);
        try {
            return sdf2.parse(s + " 23:59:59");
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将日期转换为时间戳（精确度：秒）
     */
    public static long dateToStamp(Date date) {
        return date.getTime() / 1000;
    }

    /**
     * 将日期转换为时间戳（精确度：秒）
     */
    public static long stringToStamp(String dateTime) {
        Date date = getDateByStrType(dateTime, "yyyy-MM-dd HH:mm:ss");
        return date.getTime() / 1000;
    }

    /**
     * 将日期转换为时间戳（精确度：秒）
     */
    public static int stringToStampInt(String dateTime) {
        Date date = getDateByStrType(dateTime, "yyyy-MM-dd HH:mm:ss");
        return (int) (date.getTime() / 1000);
    }

    /**
     * 将时间戳（精确度：秒）转换为日期
     */
    public static Date stampToDate(long stamp) {
        return new Date(stamp * 1000);
    }

    /**
     * 将时间戳（精确度：秒）转换为日期
     */
    public static String stampToString(long stamp, String pattern) {
        Date date = stampToDate(stamp);
        return getString(date, pattern);
    }

    public static String getString(Date date, String pattern) {
        if (null == date) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static boolean isToday(Date date) {
        Date now = new Date();
        return StringUtils.equals(getString(now, "yyyyMMdd"), getString(date, "yyyyMMdd"));
    }

    public static boolean nowAfter(String yyyyMMddHHmmss) {
        Date dateByStrType = getDateByStrType(yyyyMMddHHmmss, "yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        return now.after(dateByStrType);
    }
}
